<?php

@session_start();
include_once('Classes/Connect.php');
include('Classes/Utils.php');

global $dbSingleton;


if (isset($_POST['event_id']) && ctype_alnum($_POST['event_id'])) {
    $queryResult = $dbSingleton->query('SELECT * FROM msa_slotobject WHERE event_id="' . $_POST['event_id'] . '"');
    if (is_object($queryResult)) {
        $queryResult->setFetchMode(PDO::FETCH_OBJ);
        while ($event = $queryResult->fetch()) {
// TODO : Check integrity of prestation_id + prestation exists
// Get prestation duration
            $queryResultPrestation = $dbSingleton->query('SELECT description_courte_interne, nom, prenom, telephone_portable, duree, prestation_name, slot_color, msa_clientobject.id AS client_id FROM msa_prestationobject, msa_clientobject WHERE msa_prestationobject.id=' . $event->prestation_id . ' AND msa_clientobject.id=' . $event->client_id . ' ');
            if (is_object($queryResultPrestation)) {
                $queryResultPrestation->setFetchMode(PDO::FETCH_OBJ);
                $prestation = $queryResultPrestation->fetch();
                $event->slot_end = $event->slot_start + ($prestation->duree * 60);
                $title = $prestation->prestation_name;
                $start = ((int) $event->slot_start + (int) $event->slot_day) * 1000;
                $eventElement = array('id' => $event->event_id, 'client_id' => (int) $event->client_id, 'prestation_id' => (int) $event->prestation_id, 'slot_day' => (int) $event->slot_day, 'slot_start' => (int) $event->slot_start, 'start' => $start, 'slot_end' => (int) $event->slot_end, 'title' => $title, 'agenda_id' => (int) $event->agenda_id, 'color' => $prestation->slot_color, 'event_type' => $event->event_type, 'occurency' => 'original', 'nom_client' => $prestation->nom, 'prenom_client' => $prestation->prenom, 'telephone_client' => $prestation->telephone_portable, 'occurency' => 'original', 'status_rdv' => $event->status_rdv);
                for ($i = 1; $i <= 5; $i++) {
                    $extra_field_name = 'extra_field_' . $i;
                    if (isset($event->$extra_field_name) && strlen($event->$extra_field_name)) {
                        $eventElement[$extra_field_name] = $event->$extra_field_name;
                    }
                }
                echo json_encode($eventElement);
            }
        }
    }
}